import datetime
from typing import List, Literal, Union
from pydantic import Field, BaseModel, validator
from package.fastapi.uuid7 import uuid7_hex


# 周期定时配置
class _Interval(BaseModel):
    """周期任务"""
    interval: int = Field(..., title='执行周期', ge=0)
    unit: Literal['hours', 'minutes', 'seconds'] = Field(..., title='周期单位', description='时分', example='seconds')


class _Cron(BaseModel):
    """定时任务"""
    year: int = Field(None, title='执行年份', ge=1997, lte=2500)
    month: int = Field(None, title='执行月份', gte=1, lte=12)
    week: int = Field(None, title='执行周', gte=1, lte=53)
    day_of_week: int = Field(None, title='执行周第几天', gte=1, lte=7)
    day: int = Field(None, title='执行日期', gte=1, lte=31)
    hour: int = Field(None, title='执行时间', gte=0, lte=23)
    minute: int = Field(None, title='执行分钟', gte=0, lte=59)
    second: int = Field(None, title='执行秒', gte=0, lte=59)


class Cron(BaseModel):
    cron: Union[_Interval, _Cron] = Field(None, title='执行周期')


# HTTP请求
class Request(BaseModel):
    """请求"""
    method: str = Field('GET', title='请求方法')
    url: str = Field('', title='路径', example='https://www.baidu.com', anystr_strip_whitespace=True)
    data: dict = Field(None, title='请求参数')
    headers: dict = Field({'content-type': 'application/json'}, title='请求参数')


# 数据库认证查询
class DatabaseOption(BaseModel):
    """数据库连接参数"""
    host: str = Field(..., title='主机')
    port: str = Field(..., title='端口')
    username: str = Field(..., title='用户名')
    password: str = Field(..., title='密码')
    database: str = Field(None, title='数据库')
    query: dict = Field(None, title='可选query条件')


# kafka链接
class KafkaConsumeOption(BaseModel):
    pass


class Page(BaseModel):
    page: int = Field(1, title='页码')
    page_size: int = Field(10, title='每页条数')
    total: int = Field(0, title='每页条数')

    class Config:
        json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp() * 1000)}


class BatchId(BaseModel):
    ids: List[str] = Field(None, title='ID列表')
